home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / pygtk / 2.0 / codegen / docextract.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2006-01-20  |  6KB  |  228 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Simple module for extracting GNOME style doc comments from C
  5. sources, so I can use them for other purposes.'''
  6. import sys
  7. import os
  8. import string
  9. import re
  10. __all__ = [
  11.     'extract']
  12.  
  13. class FunctionDoc:
  14.     
  15.     def __init__(self):
  16.         self.name = None
  17.         self.params = []
  18.         self.description = ''
  19.         self.ret = ''
  20.  
  21.     
  22.     def set_name(self, name):
  23.         self.name = name
  24.  
  25.     
  26.     def add_param(self, name, description):
  27.         if name == '...':
  28.             name = 'Varargs'
  29.         
  30.         self.params.append((name, description))
  31.  
  32.     
  33.     def append_to_last_param(self, extra):
  34.         self.params[-1] = (self.params[-1][0], self.params[-1][1] + extra)
  35.  
  36.     
  37.     def append_to_named_param(self, name, extra):
  38.         for i in range(len(self.params)):
  39.             if self.params[i][0] == name:
  40.                 self.params[i] = (name, self.params[i][1] + extra)
  41.                 return None
  42.                 continue
  43.         
  44.         self.add_param(name, extra)
  45.  
  46.     
  47.     def append_description(self, extra):
  48.         self.description = self.description + extra
  49.  
  50.     
  51.     def append_return(self, extra):
  52.         self.ret = self.ret + extra
  53.  
  54.     
  55.     def get_param_description(self, name):
  56.         for param, description in self.params:
  57.             if param == name:
  58.                 return description
  59.                 continue
  60.         else:
  61.             return ''
  62.  
  63.  
  64. comment_start_pat = re.compile('^\\s*/\\*\\*\\s')
  65. comment_end_pat = re.compile('^\\s*\\*+/')
  66. comment_line_lead = re.compile('^\\s*\\*\\s*')
  67. funcname_pat = re.compile('^(\\w+)\\s*:?')
  68. return_pat = re.compile('^(returns:|return\\s+value:|returns\\s*)(.*\\n?)$', re.IGNORECASE)
  69. param_pat = re.compile('^@(\\S+)\\s*:(.*\\n?)$')
  70.  
  71. def parse_file(fp, doc_dict):
  72.     line = fp.readline()
  73.     in_comment_block = 0
  74.     while line:
  75.         if not in_comment_block:
  76.             if comment_start_pat.match(line):
  77.                 in_comment_block = 1
  78.                 cur_doc = FunctionDoc()
  79.                 in_description = 0
  80.                 in_return = 0
  81.             
  82.             line = fp.readline()
  83.             continue
  84.         
  85.         if comment_end_pat.match(line):
  86.             if not cur_doc.name:
  87.                 sys.stderr.write('no function name found in doc comment\n')
  88.             else:
  89.                 doc_dict[cur_doc.name] = cur_doc
  90.             in_comment_block = 0
  91.             line = fp.readline()
  92.             continue
  93.         
  94.         line = comment_line_lead.sub('', line)
  95.         if not line:
  96.             line = '\n'
  97.         
  98.         if not cur_doc.name:
  99.             match = funcname_pat.match(line)
  100.             if match:
  101.                 cur_doc.set_name(match.group(1))
  102.             
  103.         elif in_return:
  104.             match = return_pat.match(line)
  105.             if match:
  106.                 cur_doc.description = cur_doc.description + return_start + cur_doc.ret
  107.                 return_start = match.group(1)
  108.                 cur_doc.ret = match.group(2)
  109.             else:
  110.                 cur_doc.append_return(line)
  111.         elif in_description:
  112.             if line[:12] == 'Description:':
  113.                 line = line[12:]
  114.             
  115.             match = return_pat.match(line)
  116.             if match:
  117.                 in_return = 1
  118.                 return_start = match.group(1)
  119.                 cur_doc.append_return(match.group(2))
  120.             else:
  121.                 cur_doc.append_description(line)
  122.         elif line == '\n':
  123.             in_description = 1
  124.         else:
  125.             match = param_pat.match(line)
  126.             if match:
  127.                 param = match.group(1)
  128.                 desc = match.group(2)
  129.                 if param == 'returns':
  130.                     cur_doc.ret = desc
  131.                 else:
  132.                     cur_doc.add_param(param, desc)
  133.             else:
  134.                 
  135.                 try:
  136.                     if param == 'returns':
  137.                         cur_doc.append_return(line)
  138.                     else:
  139.                         cur_doc.append_to_last_param(line)
  140.                 except:
  141.                     sys.stderr.write('something weird while reading param\n')
  142.  
  143.         line = fp.readline()
  144.  
  145.  
  146. def parse_dir(dir, doc_dict):
  147.     for file in os.listdir(dir):
  148.         if file in ('.', '..'):
  149.             continue
  150.         
  151.         path = os.path.join(dir, file)
  152.         if os.path.isdir(path):
  153.             parse_dir(path, doc_dict)
  154.         
  155.         if len(file) > 2 and file[-2:] == '.c':
  156.             parse_file(open(path, 'r'), doc_dict)
  157.             continue
  158.     
  159.  
  160.  
  161. def extract(dirs, doc_dict = None):
  162.     if not doc_dict:
  163.         doc_dict = { }
  164.     
  165.     for dir in dirs:
  166.         parse_dir(dir, doc_dict)
  167.     
  168.     return doc_dict
  169.  
  170. tmpl_section_pat = re.compile('^<!-- ##### (\\w+) (\\w+) ##### -->$')
  171.  
  172. def parse_tmpl(fp, doc_dict):
  173.     cur_doc = None
  174.     line = fp.readline()
  175.     while line:
  176.         match = tmpl_section_pat.match(line)
  177.         if match:
  178.             cur_doc = None
  179.             sect_type = match.group(1)
  180.             sect_name = match.group(2)
  181.             if sect_type == 'FUNCTION':
  182.                 cur_doc = doc_dict.get(sect_name)
  183.                 if not cur_doc:
  184.                     cur_doc = FunctionDoc()
  185.                     cur_doc.set_name(sect_name)
  186.                     doc_dict[sect_name] = cur_doc
  187.                 
  188.             
  189.         elif line == '<!-- # Unused Parameters # -->\n':
  190.             cur_doc = None
  191.         elif cur_doc:
  192.             if line[:10] == '@Returns: ':
  193.                 if string.strip(line[10:]):
  194.                     cur_doc.append_return(line[10:])
  195.                 
  196.             elif line[0] == '@':
  197.                 pos = string.find(line, ':')
  198.                 if pos >= 0:
  199.                     cur_doc.append_to_named_param(line[1:pos], line[pos + 1:])
  200.                 else:
  201.                     cur_doc.append_description(line)
  202.             else:
  203.                 cur_doc.append_description(line)
  204.         
  205.         line = fp.readline()
  206.  
  207.  
  208. def extract_tmpl(dirs, doc_dict = None):
  209.     if not doc_dict:
  210.         doc_dict = { }
  211.     
  212.     for dir in dirs:
  213.         for file in os.listdir(dir):
  214.             if file in ('.', '..'):
  215.                 continue
  216.             
  217.             path = os.path.join(dir, file)
  218.             if os.path.isdir(path):
  219.                 continue
  220.             
  221.             if len(file) > 2 and file[-2:] == '.sgml':
  222.                 parse_tmpl(open(path, 'r'), doc_dict)
  223.                 continue
  224.         
  225.     
  226.     return doc_dict
  227.  
  228.